import {http} from "@/common/request.js";
export function fammterString(type, uuid) {
	let resString;
	let course = "course";
	let dirName = uuid
	let schoolId = uni.getStorageSync("schoolInfo").schoolId
	//1.正式课程   2.草稿   3.考试文件    type类型1，2，3
	let typeObj = {
		1: "form@lFile",
		2: "dr@ftFile",
		3: "ex@mFile",
	};
	resString = `${schoolId}/${course}/${dirName}/${typeObj[type]}`;
	console.log(resString, "resString////////////");
	return resString;
}
// 获取oss签名
export function getOssSignature(path) {
	return http.request({
		url: "/common/getOssSignature",
		method: "get",
		params: {
			path: path,
		},
	});
}
//   上传
export function returnType(name) {
	let uploadType = name.slice(name.lastIndexOf('.'), name.length)
	let imgTypes = ['.jpg', '.jpeg', '.png', '.gif', '.bmp']
	let videoTypes = ['.mp4', '.webm', '.wmv', '.avi', '.mkv']
	let wordPdfPptTypes = ['.pdf', '.doc', '.docx', '.pptx']
	//type==1  图片类型文件，type==2视频类型文件
	let type
	if (imgTypes.indexOf(uploadType) > -1) {
		type = 1
	} else if (videoTypes.indexOf(uploadType) > -1) {
		type = 2
	} else if (wordPdfPptTypes.indexOf(uploadType) > -1) {
		type = 3
	} else {
		type = 4
	}
	return type
}
export function uploadOSS(files, path, needName) {
	return new Promise((resolve, reject) => {
		var fileListCopy;
		var filesReturn;
		var dirPath;
		const currentDate = new Date();
		const year = currentDate.getFullYear();
		const month = String(currentDate.getMonth() + 1).padStart(2, "0");
		const day = String(currentDate.getDate()).padStart(2, "0");

		const formattedDate = `${year}/${month}/${day}`;
		filesReturn = [];
		let sum = 0;
		files.map((file) => {
			const fileSize = files.size / 1024 / 1024; // 转换为MB
			if (fileSize > 10) {
				uni.showToast({
					title: `文件大小不能超过${10}MB`,
					icon: 'none',
					duration: 1500
				});
				return;
			}
			// if (path) {
			//   dirPath = path;
			// } else {
			dirPath = `Zy/${file.url
            .split(".")
            .pop().toLowerCase()}/${formattedDate}`;
			// }
			
			getOssSignature(dirPath).then((res) => {
				let expire = new Date().getTime()
				uni.uploadFile({
					url: res.data.host, // 上传地址，根据实际情况替换
					filePath: file.url,
					name: 'file',
					formData: {
						key: `${dirPath}/${expire}.${file.url.split('.').pop()}`,
						OSSAccessKeyId: res.data.accessId,
						policy: res.data.policy,
						Signature: res.data.signature,
						success_action_status: 200,
					},
					success: (response) => {
						console.log('文件上传成功:', response);
						if (needName) {
							filesReturn.push({
								type: returnType(file.url),
								name: file.url,
								url: `${res.data.url}/${expire}.${file.url.split('.').pop()}`,
							});
						} else {
							filesReturn.push({
								url: `${res.data.url}/${expire}.${file.url.split('.').pop()}`,
								name: file.url,
							});
						}

						sum++;
						if (sum == files.length) {
							resolve(filesReturn);
						}
					},
					fail: (err) => {
						console.log('文件上传失败:', err);
					}
				});

			});
		})
	});
}